package com.zxhhyj.ui.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.LocalContentColor
import androidx.compose.material.LocalTextStyle
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlin.math.abs
import kotlin.math.roundToInt

val toolbarHeight = 56.dp

class TopBarState : NestedScrollConnection {

    /**
     * TopBar高度
     */
    var topBarHeight by mutableIntStateOf(0)

    /**
     * TopBar可活动的长度
     */
    var topBarActiveLength by mutableIntStateOf(0)

    /**
     * TopBar当前偏移高度
     */
    var tapBarOffsetHeight by mutableFloatStateOf(0f)

    /**
     * topBar偏移进度
     */
    val progress get() = abs(tapBarOffsetHeight) / topBarActiveLength

    override fun onPostScroll(
        consumed: Offset,
        available: Offset,
        source: NestedScrollSource
    ): Offset {
        val newOffset = tapBarOffsetHeight + available.y
        tapBarOffsetHeight = newOffset.coerceIn(tapBarOffsetHeight, 0f)
        return super.onPostScroll(consumed, available, source)
    }

    override fun onPreScroll(
        available: Offset,
        source: NestedScrollSource
    ): Offset {
        val newOffset = tapBarOffsetHeight + available.y
        return if (available.y > 0) {
            Offset.Zero
        } else {
            tapBarOffsetHeight = newOffset.coerceIn(-topBarActiveLength.toFloat(), tapBarOffsetHeight)
            when {
                newOffset < tapBarOffsetHeight -> {
                    Offset.Zero
                }

                newOffset > -topBarHeight -> {
                    available
                }

                else -> {
                    Offset.Zero
                }
            }
        }
    }
}

internal fun Modifier.bindTopBarState() = composed {
    val topBarState = _root_ide_package_.com.zxhhyj.ui.view.LocalTopBarState.current
    nestedScroll(connection = topBarState).graphicsLayer(translationY = topBarState.topBarHeight + topBarState.tapBarOffsetHeight)
}

@Composable
fun AppCenterTopBar(
    modifier: Modifier = Modifier,
    actions: (@Composable () -> Unit)? = null,
    title: @Composable () -> Unit = {},
) {
    val topBarState = _root_ide_package_.com.zxhhyj.ui.view.LocalTopBarState.current
    Box(
        modifier = modifier
            .fillMaxWidth()
            .onSizeChanged {
                topBarState.topBarHeight = it.height
            }
            .clickable(enabled = false) {
                //用户修复点击事件穿透的问题
            }
            .background(_root_ide_package_.com.zxhhyj.ui.theme.LocalColorScheme.current.background)
            .padding(horizontal = _root_ide_package_.com.zxhhyj.ui.theme.LocalDimens.current.horizontal)
    ) {
        Box(
            modifier = _root_ide_package_.androidx.compose.ui.Modifier.Companion
                .height(_root_ide_package_.com.zxhhyj.ui.view.toolbarHeight)
                .fillMaxWidth(),
            contentAlignment = Alignment.Center,
            content = {
                CompositionLocalProvider(
                    LocalTextStyle provides TextStyle(
                        color = _root_ide_package_.com.zxhhyj.ui.theme.LocalColorScheme.current.text,
                        fontSize = 18.sp
                    )
                ) {
                    title()
                }
            }
        )
        Row(
            modifier = Modifier.align(Alignment.CenterEnd),
            horizontalArrangement = Arrangement.spacedBy(_root_ide_package_.com.zxhhyj.ui.theme.LocalDimens.current.horizontal / 2)
        ) {
            actions?.let {
                CompositionLocalProvider(LocalContentColor provides _root_ide_package_.com.zxhhyj.ui.theme.LocalColorScheme.current.highlight) {
                    it.invoke()
                }
            }
        }
    }
}

@Composable
fun AppTopBar(
    modifier: Modifier = Modifier,
    topBarProperties: com.zxhhyj.ui.view.TopBarProperties = _root_ide_package_.com.zxhhyj.ui.view.TopBarProperties(),
    title: @Composable () -> Unit = {},
    actions: @Composable () -> Unit = {},
    content: @Composable () -> Unit = {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = _root_ide_package_.com.zxhhyj.ui.theme.LocalDimens.current.horizontal)
                .height(_root_ide_package_.com.zxhhyj.ui.view.toolbarHeight)
        ) {
            CompositionLocalProvider(
                LocalTextStyle provides TextStyle(
                    color = _root_ide_package_.com.zxhhyj.ui.theme.LocalColorScheme.current.text,
                    fontSize = 26.sp,
                )
            ) {
                title()
            }
        }
    }
) {
    val topBarState = _root_ide_package_.com.zxhhyj.ui.view.LocalTopBarState.current
    var contentSize by remember {
        mutableStateOf(IntSize.Zero)
    }
    Box(modifier = modifier) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .onSizeChanged {
                    topBarState.topBarHeight = it.height
                    topBarState.topBarActiveLength = it.height / 2
                }) {
            Box(
                modifier = Modifier
                    .clickable(enabled = false) {
                        //用户修复点击事件穿透的问题
                    }
                    .height(_root_ide_package_.com.zxhhyj.ui.view.toolbarHeight)
                    .fillMaxWidth()
                    .clipToBounds()
                    .background(_root_ide_package_.com.zxhhyj.ui.theme.LocalColorScheme.current.background)
                    .padding(horizontal = _root_ide_package_.com.zxhhyj.ui.theme.LocalDimens.current.horizontal),
                contentAlignment = Alignment.CenterEnd,
                content = {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .offset {
                                return@offset IntOffset(
                                    x = 0,
                                    y = (contentSize.height + topBarState.tapBarOffsetHeight)
                                        .roundToInt()
                                        .coerceIn(0, contentSize.height)
                                )
                            },
                        contentAlignment = Alignment.Center
                    ) {
                        CompositionLocalProvider(
                            LocalTextStyle provides TextStyle(
                                color = _root_ide_package_.com.zxhhyj.ui.theme.LocalColorScheme.current.text,
                                fontSize = 18.sp,
                            )
                        ) {
                            title()
                        }
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(_root_ide_package_.com.zxhhyj.ui.theme.LocalDimens.current.horizontal / 2)) {
                        CompositionLocalProvider(LocalContentColor provides _root_ide_package_.com.zxhhyj.ui.theme.LocalColorScheme.current.highlight) {
                            actions.invoke()
                        }
                    }
                }
            )
            if (topBarProperties.showDivider) {
                _root_ide_package_.com.zxhhyj.ui.view.AppDivider(
                    modifier = Modifier
                        .fillMaxWidth()
                        .alpha(topBarState.progress)
                )
            }
            Box(
                modifier = Modifier
                    .clipToBounds()
                    .onSizeChanged {
                        contentSize = it
                    }
                    .offset {
                        return@offset IntOffset(
                            x = 0,
                            y = topBarState.tapBarOffsetHeight.roundToInt()
                        )
                    }
                    .clickable(enabled = false) {
                        //用户修复点击事件穿透的问题
                    }
            ) {
                content.invoke()
            }
        }

    }
}

/**
 * @param showDivider 是否显示中间的线条
 */
class TopBarProperties(val showDivider: Boolean = true)